#
# @lc app=leetcode.cn id=199 lang=python3
#
# [199] 二叉树的右视图
#

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
import collections
from typing import List, Optional

# @lc code=start
# Definition for a binary tree node.
class Solution:
    def rightSideView(self, root: Optional[TreeNode]) -> List[int]:
        if root is None:
            return []
        ans = []
        queue = collections.deque()
        queue.append(root)
        while queue:
            size = len(queue)
            while size > 0:
                size -= 1
                node = queue.popleft()
                if size == 0:
                    ans.append(node.val)
                if node.left:
                    queue.append(node.left)
                if node.right:
                    queue.append(node.right)
        return ans
# @lc code=end

